package cn.ikun.mf.jdbc;

import java.sql.*;
import java.util.Scanner;

public class AppLogin {
    public static void main(String[] args)throws Exception {
        Scanner scanner=new Scanner(System.in);
        System.out.printf("请输入账号：");
        String username = scanner.nextLine();
        System.out.printf("请输入密码：");
        String password = scanner.nextLine();
        LoginPuls(username,password);
    }
    public static void Login(String n,String p) throws Exception{
        System.out.println("输入的账号是："+n);
        System.out.println("输入的密码是："+p);
        String sql="SELECT 1 FROM `user`WHERE username='" +n+
                "' AND `password`=MD5('" +p+
                "');";
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/test?useSSL=false";
        String username="root";
        String password="5240";
        Connection connection = DriverManager.getConnection(url, username, password);
        Statement statement = connection.createStatement();
        ResultSet set = statement.executeQuery(sql);
        if(set.next()){
            System.out.println("登陆成功!");
        }else {
            System.out.println("登陆失败!");
        }

    }
    public static void LoginPuls(String n,String p)throws Exception{
        System.out.println("输入的账号是:"+n);
        System.out.println("输入的密码是:"+p);
        String sql="SELECT id FROM `user`\n" +
                "WHERE username = ? AND `password` = MD5(?)";
        System.out.println("输入的sql是:"+sql);
        Class.forName("com.mysql.jdbc.Driver");
        String url ="jdbc:mysql://localhost:3306/test?useSSL=false";
        String username = "root";
        String password = "5240";
        Connection connection = DriverManager.getConnection(url, username, password);
        //使用connection.prepareStatement(sql)方法创建一个预对象
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //为第一个？传值
        preparedStatement.setString(1,n);
//        为第二个？传值
        preparedStatement.setString(2,p);
        ResultSet set = preparedStatement.executeQuery();
        if (set.next()){
            System.out.println("登陆成功");
        }else {
            System.out.println("登陆失败");
        }
    }
}
